package cn.ht.jywrwater.module.ht.controller.admin.town;

import cn.ht.jywrwater.framework.common.pojo.CommonResult;
import cn.ht.jywrwater.framework.common.pojo.PageResult;
import cn.ht.jywrwater.framework.excel.core.util.ExcelUtils;
import cn.ht.jywrwater.framework.operatelog.core.annotations.OperateLog;
import cn.ht.jywrwater.module.ht.controller.admin.town.vo.*;
import cn.ht.jywrwater.module.ht.convert.town.TownConvert;
import cn.ht.jywrwater.module.ht.dal.dataobject.town.TownDO;
import cn.ht.jywrwater.module.ht.service.town.TownService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.Collection;
import java.util.List;

import static cn.ht.jywrwater.framework.common.pojo.CommonResult.success;
import static cn.ht.jywrwater.framework.operatelog.core.enums.OperateTypeEnum.EXPORT;

@Tag(name = "管理后台 - 乡镇管理")
@RestController
@RequestMapping("/ht/town")
@Validated
public class TownController {

    @Resource
    private TownService townService;

    @PostMapping("/create")
    @Operation(summary = "创建乡镇管理")
    @PreAuthorize("@ss.hasPermission('ht:town:create')")
    public CommonResult<Long> createTown(@Valid @RequestBody TownCreateReqVO createReqVO) {
        return success(townService.createTown(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新乡镇管理")
    @PreAuthorize("@ss.hasPermission('ht:town:update')")
    public CommonResult<Boolean> updateTown(@Valid @RequestBody TownUpdateReqVO updateReqVO) {
        townService.updateTown(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除乡镇管理")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('ht:town:delete')")
    public CommonResult<Boolean> deleteTown(@RequestParam("id") Long id) {
        townService.deleteTown(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得乡镇管理")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('ht:town:query')")
    public CommonResult<TownRespVO> getTown(@RequestParam("id") Long id) {
        TownDO town = townService.getTown(id);
        return success(TownConvert.INSTANCE.convert(town));
    }

    @GetMapping("/list")
    @Operation(summary = "获得乡镇管理列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('ht:town:query')")
    public CommonResult<List<TownRespVO>> getTownList(@RequestParam("ids") Collection<Long> ids) {
        List<TownDO> list = townService.getTownList(ids);
        return success(TownConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得乡镇管理分页")
    @PreAuthorize("@ss.hasPermission('ht:town:query')")
    public CommonResult<PageResult<TownRespVO>> getTownPage(@Valid TownPageReqVO pageVO) {
        PageResult<TownDO> pageResult = townService.getTownPage(pageVO);
        return success(TownConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出乡镇管理 Excel")
    @PreAuthorize("@ss.hasPermission('ht:town:export')")
    @OperateLog(type = EXPORT)
    public void exportTownExcel(@Valid TownExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<TownDO> list = townService.getTownList(exportReqVO);
        // 导出 Excel
        List<TownExcelVO> datas = TownConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "乡镇管理.xls", "数据", TownExcelVO.class, datas);
    }

}
